package com.demo.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.redis.core.ListOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.SetOperations;
import org.springframework.stereotype.Service;

import redis.clients.jedis.Jedis;

import com.demo.common.DateUtil;
import com.demo.common.JsonUtil;
import com.demo.config.RedisClient1;
import com.demo.dao.UserDao;
import com.demo.entity.User;
import com.demo.service.UserService;


/**
 * 
 * @ClassName:  UserServiceImpl   
 * @Description:user������   
 * @author: chenghao  
 * @date:   2017-12-12 ����2:31:23   
 *
 */
@Service
public class UserServiceImpl implements UserService{
	@Autowired
	private UserDao userDao;
	@Autowired
	private RedisClient1 redisClient1;
	//几种redis使用根据你的情况随便使用
	/*@Autowired
	private RedisTemplate redisTemplate;*/

	String key1  = "user_login";


	
	/**
	 * 
	 * <p>Title: getUserList</p>   
	 * <p>Description:��ȡ�û��б� </p>   
	 * @return   
	 * @see com.ch.service.UserService#getUserList()
	 */
	public List<User> getUserList() {
		List<User> userList = new ArrayList<User>();
		userList = userDao.findAll();
		return userList;
	}

	public User findUserById(Integer id) {
		User user = new User();
		user = userDao.findById(id);
		return user;
	}

	public void save(User user) {
		user.setRegTime(DateUtil.getDateFormat(new Date()));
		userDao.save(user);
	}

	public void edit(User user) {
		user.setRegTime(userDao.findById(user.getId()).getRegTime());
		userDao.save(user);
		
	}

	public void delete(Integer id) {
		userDao.delete(id);
		
	}
	/**
	 * 
	 * <p>Title: findByUserNameAndPassword</p>   
	 * <p>Description: ����û��������ѯ�û�</p>   
	 * @param userName
	 * @param passWord
	 * @return   
	 * @see com.ch.service.UserService#findByUserNameAndPassword(java.lang.String, java.lang.String)
	 */
	public User findByUserNameAndPassword(String userName, String passWord) {
		User user = new User();
		//也可以直接在Clinet里直接写入判断是否存在的方法，这里我没有写入就直接根据结果string判断了
		String userjson = redisClient1.getData(key1);

		if (userjson!=null) {
			user = JsonUtil.parse(userjson, User.class);
		} else {
			user = userDao.findByUserNameAndPassWord(userName, passWord);
			if(user!=null){
				redisClient1.setData(key1,JsonUtil.toJSON(user));
			}
		}
			
		return user;
	}

	public Page<User> findALL(Pageable pageable) {
		
		return userDao.findAll(pageable);
	}

}
